package com.wbsxch.algorithm01;

import org.junit.Test;

import java.util.Scanner;

public class 二分查找 {

    private static int rank(int value,int[] array){
        int start = 0;
        int end = array.length - 1;

        while (start <= end){
            int middle = (start+end)/2;
            if(value < array[middle]) end = middle-1;
            else if(value > array[middle]) start = middle+1;
            else return middle;
        }
        return -1;
    }

    public static void main(String[] args) {
        /*int[] arr = {1,54,236,89,45,76,49,785,14};
        Arrays.sort(arr);
        int rank = rank(785, arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("%d\t",arr[i]);
        }
        System.out.printf("\n%d",rank);*/
        // method05();
        method06();
    }
    int[] number100W;
    int[] number1000W;
    private void method0102SetUp(){
        number100W = Input.readFileAndSort("D:\\admin\\文档\\算法\\largeW.txt");
        System.out.println(number100W.length);
        number1000W = Input.readFileAndSort("D:\\admin\\文档\\算法\\largeT.txt");
        System.out.println(number1000W.length);

        System.out.println("以下数字与任何客户无关：");
    }

    @Test
    public void method01() throws Exception{
        method0102SetUp();
        for (int i = 0; i < number100W.length; i++) {
            int i1 = number100W[i];
            if(rank(i1, number1000W) < 0){
                System.out.println(i1);
            }
        }
    }

    @Test
    public void method02() throws Exception{
        method0102SetUp();
        for (int i = 0; i < number100W.length; i++) {
            int i1 = number100W[i];
            if(rank02(i1, number1000W) < 0){
                System.out.println(i1);
            }
        }
    }

    public int rank02(int value, int[] array){
        for (int i = 0; i < array.length; i++) {
            if(value == array[i]) return i;
        }
        return -1;
    }

    @Test
    public void method03() {
        System.out.println((int)Math.abs(-2147483648*2L+1));
        System.out.println(Double.POSITIVE_INFINITY);
        System.out.println(Double.NEGATIVE_INFINITY);

        double a = 0.5;
        float b = (float)a;
        System.out.println(-2147483648*3);
    }

    @Test
    public void method04() {
        System.out.println((0+15)/2);//7
        System.out.println(2.0e-6 * 100000000.1);
        System.out.println(true && false || true && true);//true
        System.out.println((1+2.236)/2);//1.618
        System.out.println(1+2+3+4.0);//10.0
        System.out.println(4.1 >= 4);//true
        System.out.println(1+2+"3");//33
    }

    public static void method05() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入第一个整数参数:");
        int a = scanner.nextInt();
        System.out.println("请输入第二个整数参数:");
        int b = scanner.nextInt();
        System.out.println("请输入第三个整数参数:");
        int c = scanner.nextInt();

        if(a == b && a == c && b == c){
            System.out.println("equal");
        }else {
            System.out.println("not equal");
        }
    }


    public static void method06() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入a:");
        double a = scanner.nextDouble();
        System.out.println("请输入b:");
        double b = scanner.nextDouble();
        if(0 < a && a < 1 && 0 < b && b < 1){
            System.out.println(true);
        }else {
            System.out.println(false);
        }
    }

    @Test
    public void name01() {
        /*int f = 0;
        int g = 1;
        for (int i = 0; i <= 15; i++) {
            System.out.println(f);
            f = f + g;
            System.out.printf("f:%s",f);
            g = f - g;
            System.out.printf("g:%s\n",g);
        }*/

        /*double t = 9.0;
        while(Math.abs(t-9.0/t) > .001)
            t = (9.0/t + t) / 2.0;

        System.out.printf("%.5f\n",t);*/

        int sum = 0;
        for (int i = 1; i < 1000; i++) {
            for (int j = 0; j < i; j++) {
                sum++;
            }
        }
        System.out.println(sum);
    }

    @Test
    public void name02() {
        int sum = 0;
        for (int i = 1;i<1000;i++){
            sum+=i;
        }
        System.out.println(sum);
    }

    @Test
    public void name03() {
        int sum = 0;
        for (int i = 1;i<1000;i *= 2){
            for (int j=0;j<1000;j++){
                sum++;
            }
        }
        System.out.println(sum);
    }

    @Test
    public void name04() {
        System.out.println('b');
        System.out.println('b'+'c');
        System.out.println((char) ('a'+4));
        System.out.println((int) ('a'));
    }

    @Test
    public void name05() {
        int a = 5412;
        String s = Integer.toBinaryString(a);
        System.out.println(s);

        String s1 = "";
        for (int i = a;i>0;i /= 2){
            System.out.printf("%s \t",i);
            s1 = (i%2)+s1;
        }
        System.out.println(s1);
    }

    @Test
    public void name06() {
        boolean[][] b = {{true,false,true},{false,false,true}};

        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < b[i].length; j++) {
                String s = "";
                if(b[i][j]){
                    s = "*";
                }else {
                    s = " ";
                }
                System.out.printf("第%s行第%s列的值为：%s\n",i+1,j+1,s);
            }
        }

    }

    @Test
    public void name07() {
        int[] a = new int[10];
        for (int i=0;i<10;i++){
            a[i] = 9-i;
        }
        for (int i=0;i<10;i++){
            a[i] = a[a[i]];
        }
        for (int i=0;i<10;i++){
            System.out.println(a[i]);
        }
    }
}
